package scenes;

import awe6.core.Scene;
import awe6.extras.gui.Text;
import awe6.interfaces.EScene;
import awe6.interfaces.ETextStyle;
import awe6.interfaces.IKernel;

import entities.Bunny;

import nme.display.Bitmap;
import nme.display.BitmapData;
import nme.display.Graphics;
import nme.display.Sprite;
import nme.display.StageQuality;
import nme.display.Tilesheet;
import nme.display.BlendMode;
import nme.events.Event;
import nme.events.MouseEvent;
import nme.geom.Point;
import nme.geom.Rectangle;
import nme.Assets;
import nme.Lib;
import nme.text.TextField;
import nme.text.TextFieldAutoSize;
import nme.text.TextFormat;
import nme.text.TextFormatAlign;

/**
 * ...
 * @author Andrew Ekeren
 */
	
class TileTest extends AScene {		
	
	
	var tf:Text;	
	var numBunnies:Int;
	var incBunnies:Int;
	var smooth:Bool;
	var gravity:Float;
	var bunnies:Array <Bunny>;
	var maxX:Int;
	var minX:Int;
	var maxY:Int;
	var minY:Int;
	var drawList:Array<Float>;
	
	
	override private function _init():Void {
		
		super._init();
		
		trace ( "Entering TileTest" );
		
		// Configuration parameters
		
		isPauseable = true;
		isDisposable = true;
		isSessionSavedOnNext = true;
		
		_title.text = "TEST";
		
		gravity = 0.5;
		incBunnies = 100;
		#if flash
		smooth = false;
		numBunnies = 100;
		Lib.current.stage.quality = StageQuality.LOW;
		#else
		smooth = true;
		numBunnies = 1000;
		#end
		
		bunnies = new Array<Bunny>();
		drawList = new Array<Float>();
		
		var bunny; 
		for (i in 0...numBunnies)  {
			
			bunny = new Bunny ( _kernel );
			bunnies.push ( bunny );
			
		}
		
		// Replace later with a windowResized function
		maxX = Lib.current.stage.stageWidth;
		maxY = Lib.current.stage.stageHeight;
		minX = 0;
		minY = 0;
		
	}
	
	
	
	
	
	// Events
	
	
	
	
	
	private function updateBunnies():Void  	{
		
		var TILE_FIELDS = 6; // x+y+index+scale+rotation+alpha
		
		var bunny : Bunny;
	 	for (i in 0...numBunnies) {
			
			bunny = bunnies[i];
			bunny.position.x += bunny.speedX;
			bunny.position.y += bunny.speedY;
			bunny.speedY += gravity;
			bunny.alpha = 0.3 + 0.7 * bunny.position.y / maxY;
			
			if (bunny.position.x > maxX) {
				bunny.speedX *= -1;
				bunny.position.x = maxX;
			}
			else if (bunny.position.x < minX) {
				
				bunny.speedX *= -1;
				bunny.position.x = minX;
				
			}
			
			if (bunny.position.y > maxY) {
				
				bunny.speedY *= -0.8;
				bunny.position.y = maxY;
				if (Math.random() > 0.5) bunny.speedY -= 3 + Math.random() * 4;
				
			} 
			else if (bunny.position.y < minY) {
				
				bunny.speedY = 0;
				bunny.position.y = minY;
				
			}
			
			var index = i * TILE_FIELDS;
			drawList[index] = bunny.position.x;
			drawList[index + 1] = bunny.position.y;
			//drawList[index + 2] = 0; // sprite index
			drawList[index + 3] = bunny.scale;
			drawList[index + 4] = bunny.rotation;
			drawList[index + 5] = bunny.alpha;
			
			bunny.updateAnim (drawList, smooth);
			
		}
		
	}
	
	
	function createCounter()
	{
		var format = new TextFormat("_sans", 20, 0, true);
		format.align = TextFormatAlign.RIGHT;
		
		tf = new Text( _kernel, _kernel.factory.width, 35);
		tf.textStyle = _kernel.factory.createTextStyle( ETextStyle.HEADLINE );
		tf.text = "Bunnies:\n" + numBunnies;
		tf.x = maxX - tf.width - 10;
		tf.y = 10;
		addEntity( tf, true, 999);
		
	}
	
	/*function counter_click(e)
	{
		if (numBunnies >= 1500) incBunnies = 250;
		var more = numBunnies + incBunnies;
		
		var bunny; 
		for (i in numBunnies...more) {
			
			bunny = new Bunny( _kernel );
			bunny.position = new Point();
			bunny.speedX = Math.random() * 5;
			bunny.speedY = (Math.random() * 5) - 2.5;
			bunny.scale = 0.3 + Math.random();
			bunny.rotation = 15 - Math.random() * 30;
			bunnies.push (bunny);
			
		}
		
		numBunnies = more;
		
		tf.text = "Bunnies:\n" + numBunnies;
		stage_resize(null);
		
	}
	
	
	function stage_resize(e)  {
		
		maxX = Lib.current.stage.stageWidth;
		maxY = Lib.current.stage.stageHeight;
		tf.x = maxX - tf.width - 10;
		
		trace ( maxX );
		
	}*/
	
	
	override private function _disposer():Void {
		
		// extend here
		super._disposer();		
		
	}
	
	
	
	
	// Entry point
	
	
	
	
	
	override private function _updater( ?p_deltaTime:Int = 0 ):Void {
		
		super._updater( p_deltaTime );
		// extend here
		
		updateBunnies ();
		
	}
	
}